package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.Vaccinator;
import com.cl.service.impl.VaccinatorServiceImpl;
import com.cl.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class VaccinatorController {
    @Autowired
    private VaccinatorServiceImpl vaccinatorService;
    @Autowired
    private TimeUtil timeUtil;
    @RequestMapping(value = "/getVaccinator",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    public String getVaccinator(@RequestParam(value = "time",required = false)String time,@RequestParam(value = "name",required = false)String name, @RequestParam("page") int page, @RequestParam("limit") int limit){
        List<Vaccinator> vaccinators = new ArrayList<>();
        int i = vaccinatorService.countAll();
        if (name==null&&time!=null){
            switch (time){
                case "all":
                    vaccinators = vaccinatorService.queryAll(null,(page-1)*limit,limit,name);
                    break;
                case "today":
                    String todayTime = timeUtil.getTodayTime();
                    vaccinators = vaccinatorService.queryAll(todayTime,(page-1)*limit,limit,name);
                    break;
                case "tomorrow":
                    String tomorrowTime = timeUtil.getTomorrowTime();
                    vaccinators = vaccinatorService.queryAll(tomorrowTime,(page-1)*limit,limit,name);
            }
        }else {
            vaccinators = vaccinatorService.queryAll(null,(page-1)*limit,limit,name);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",i);
        map.put("code",0);
        map.put("msg","");
        map.put("data",vaccinators);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }
}
